import { Request, Response } from "express";
import { ModelArticle } from "../models/ModelArticle";

interface AuthRequest extends Request {
    userId?: string
}

// 查找所有文章
export const getArticles = async (req: Request, resp: Response) => {
    const articles = await ModelArticle.find()
    resp.json(articles)
}

// 根据ID查找文章
export const getArticle = async (req: Request, resp: Response) => {
    const article = await ModelArticle.findById(req.params.id)
    resp.json(article)
}

// 添加文章
export const createArticle = async (req: AuthRequest, resp: Response) => {
    const { title, content, preview } = req.body
    const article = new ModelArticle({ title, content, preview, createBy: req.userId })
    await article.save()
    resp.json(article)
}

// 删除文章
export const deleteArticle = async (req: AuthRequest, resp: Response) => {
    const article = await ModelArticle.findByIdAndDelete(req.params.id)
    resp.json(article)
}